package com.example.project.controller;

import java.util.ArrayList;
import java.util.List;

import com.example.project.entity.*;
import com.example.project.mapper.ActivityMapper;
import com.example.project.mapper.MuseumMapper;
import com.example.project.mapper.UserMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.project.mapper.BookingMapper;

/**
 * 预约信息Controller
 *
 * @author changyao
 * @date 2024-12-23
 */
@RestController
@RequestMapping("/project/booking")
public class BookingController {
    @Autowired
    private BookingMapper bookingMapper;
    @Autowired
    private MuseumMapper museumMapper;


    @Autowired
    private ActivityMapper activityMapper;
    @Autowired
    private UserMapper userMapper;

    /**
     * 查询预约信息列表
     */
    @GetMapping("/list")
    public List<BookingVo> list() {
        QueryWrapper<Booking> queryWrapper = new QueryWrapper<>();
        List<Booking> bookings = bookingMapper.selectList(queryWrapper);
        List<BookingVo> bookingVos = new ArrayList<>();

        for (Booking booking : bookings) {

            BookingVo vo = new BookingVo();
            BeanUtils.copyProperties(booking,vo);
            User user = userMapper.selectById(booking.getUserId());

            Long goodsId = booking.getGoodsId();
            vo.setUserName(user.getUserNickname());
            if (booking.getType().equals("体验馆")) {
                Museum museum = museumMapper.selectById(goodsId);
                vo.setMuseum(museum);

                vo.setGoodsName(museum.getName());
                vo.setGoodsImg(museum.getPic());
                vo.setDetails(museum.getContent());
                vo.setPrice(String.valueOf(museum.getPrice()));
                bookingVos.add(vo);
            } else {
                Activity activity = activityMapper.selectById(goodsId);
                vo.setActivity(activity);
                vo.setGoodsName(activity.getName());
                vo.setGoodsImg(activity.getPic());
                vo.setDetails(activity.getContent());
                vo.setPrice(String.valueOf(activity.getPrice()));
                bookingVos.add(vo);
            }




        }
        return bookingVos;
    }


    /**
     * 获取预约信息详细信息
     */
    @PostMapping("/detil")
    public Booking getInfo(@RequestBody Booking booking) {
        return bookingMapper.selectById(booking);
    }


    /**
     * 新增预约信息
     */
    @PostMapping("/add")
    public int add(@RequestBody Booking booking) {

        QueryWrapper<Booking> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", booking.getUserId()).eq("goods_id", booking.getGoodsId()).eq("type", booking.getType());
        Booking booking1 = bookingMapper.selectOne(queryWrapper);

        if (booking1 != null) {
            return 0;
        } else {
            bookingMapper.insert(booking);
            return 1;
        }
    }

    /**
     * 修改预约信息
     */
    @PostMapping("/edit")
    public int edit(@RequestBody Booking booking) {
        return bookingMapper.updateById(booking);
    }

    /**
     * 删除预约信息
     */
    @PostMapping("/del")
    public int remove(@RequestBody Booking booking) {
        return bookingMapper.deleteById(booking);
    }
}
